#include <Windows.h>
#include <stdio.h>
// #include <iostream>

#include "../mcu/mo_config.h"
#include "../mcu/mo_event.h"
#include "../mo_core/mo_heap.h"
#include "../mo_core/mo_msg.h"
#include "../mo_input/mo_button.h"

/*********************Task 1***************************/
typedef struct app_task1 {
    mo_task_data tsk;
} APP_TASK1_T;

static APP_TASK1_T tsk1;
void app_task1_handle(mo_task tsk, mo_msg_id id, mo_msg msg) {
    switch (id) {
        case SYS_EVENT_LED_REFRESH:

            break;
        case SYS_EVENT_VK_UP:
            printf("SYS_EVENT_MFB=%d", *(mo_u8 *)msg);
            break;
        case SYS_EVENT_VK_DOWN:
            //	printf("SYS_EVENT_MFB_UP");
            break;
        default:
            break;
    }
}

void app_task1_init(void) { tsk1.tsk.handler = app_task1_handle; }
const mo_task get_tsk1(void) { return &tsk1.tsk; }
/**************************************************************/

/*********************Task 2***************************/
typedef struct app_task2 {
    mo_task_data tsk;
    mo_u8 led;
} APP_TASK2_T;

static APP_TASK2_T tsk2 = {0};
void app_task2_handle(mo_task tsk, mo_msg_id id, mo_msg msg) {
    switch (id) {
        case SYS_KEY_EVENT: {
            mo_key_event_msg_t *key_msg = (mo_key_event_msg_t *)msg;
            if (key_msg != NULL) {
                printf("--vk_code:%d, vk_patt:%d, click_num:%d-\n", key_msg->vk_code, key_msg->vk_patt, key_msg->click_num);
            }

        }

        break;

        default:
            break;
    }
}

void app_task2_init(void) { tsk2.tsk.handler = app_task2_handle; }

const mo_task get_tsk2(void) { return &tsk2.tsk; }
/**************************************************************/

/************************************/
DWORD CALLBACK usr_thread(PVOID pvoid) {
    // MSG msg;
    mo_u8 key_status = 0;
    mo_u8 *msg = NULL;
    // char c = '0';
    HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
    INPUT_RECORD key_rec;
    DWORD dw_res;
    mo_u8 press = 0xff, pre2 = 0xff;
    while (1) {
        ReadConsoleInput(hIn, &key_rec, 1, &dw_res);

        if (key_rec.EventType == KEY_EVENT) {
            switch (key_rec.Event.KeyEvent.wVirtualKeyCode) {
                case VK_UP:
                    if (key_rec.Event.KeyEvent.bKeyDown) {
                        if (press != key_rec.Event.KeyEvent.bKeyDown) {
                            press = key_rec.Event.KeyEvent.bKeyDown;
                            mo_send_a_vk_to_btn_task(SYS_EVENT_VK_DOWN, VK_VOL_UP);
                        }

                    } else {
                        if (press != key_rec.Event.KeyEvent.bKeyDown) {
                            press = key_rec.Event.KeyEvent.bKeyDown;
                            mo_send_a_vk_to_btn_task(SYS_EVENT_VK_UP, VK_VOL_UP);
                        }
                    }

                    break;
                case VK_DOWN:
                    if (key_rec.Event.KeyEvent.bKeyDown) {
                        if (pre2 != key_rec.Event.KeyEvent.bKeyDown) {
                            pre2 = key_rec.Event.KeyEvent.bKeyDown;
                            mo_send_a_vk_to_btn_task(SYS_EVENT_VK_DOWN, VK_VOL_DOWN);
                        }
                    } else {
                        if (pre2 != key_rec.Event.KeyEvent.bKeyDown) {
                            pre2 = key_rec.Event.KeyEvent.bKeyDown;
                            mo_send_a_vk_to_btn_task(SYS_EVENT_VK_UP, VK_VOL_DOWN);
                        }
                    }
                    break;
                default:
                    break;
            }
        }

#if 0
		//printf(" %c pressed",c);
		switch (c){
			case 'q': /* MFB Key Down**/
				mo_msg_send(get_tsk1(),SYS_EVENT_MFB_DOWN,NULL,0);
				break;
			case 'w': /* MFB Key Up**/
				mo_msg_send(get_tsk1(),SYS_EVENT_MFB_UP,NULL,0);
				break;
			default:
				printf(" %c pressed",c);
				break;
		}
#endif
    }
    return 0;
}
/****************************************************************************/

void key_polling_task(void) { /*for 1ms systick timer*/

    DWORD dwThreadId = 0;
    HANDLE hthread = CreateThread(NULL, 0, usr_thread, 0, 0, &dwThreadId);
}

/****************************************/

int main(int argc, char **argv) {
    mo_msg_init();
    key_polling_task();
    app_task1_init();
    app_task2_init();

    // printf("get_tsk2().tsk.handle = %d",get_tsk2()->handler);
    // mo_msg_send(get_tsk2(),SYS_EVENT_LED_REFRESH,NULL,10);

    mo_btn_init(get_tsk2());
    mo_msg_loop(NULL);

    return 0;
}
